<?php
include_once('general_include.php');
if($_REQUEST['action']=='sendreview')
{
	
	$num_coach_rating_type=$_REQUEST['NumCoachRatingCat'];
	$coach_id = $_POST['coach_id'];
	$user_type_id=$_POST['user_type_id'];
	$review_comment = $_POST['review_comment'];
	$reviewed_by=$_POST['reviewed_by'];
	$total_rating=0;
	
	for($i=0;$i<$num_coach_rating_type;$i++){
		
		$get_rating[$i]=$_REQUEST['Rating_'.$i.''];
		$get_comment[$i]=$_REQUEST['Comment_'.$i.''];
		$explode_str=explode(":",$get_rating[$i]);
		$rating[$i]=$explode_str[0];
		$rating_cat_id[$i]=$explode_str[1];
		$store_id[$i]=$_REQUEST['comment_rating_'.$i.''];
	}
	
	$overall_comment=$_REQUEST['Comment_overall'];
	$overall_rating=$_REQUEST['Rating_overall'];
	
	$CoachReviewSql="INSERT INTO ".TABLEPREFIX."_user_review 
					(
					user_id,
					user_type_id,
					reviewed_by,
					review_comment,
					rating,
					is_active,
					date_added
					)
					VALUES
					(
					'".$coach_id."',
					'".$user_type_id."',
					'".$reviewed_by."',
					'".$review_comment."',
					'".$overall_rating."',
					'Y',
					'".date('Y-m-d H:i:s')."'
					)";
	$CoachReview=$UserManagerObjAjax->Execute($CoachReviewSql);
	$user_review_id=mysql_insert_id();
	
	if($num_coach_rating_type>0){
	
		for($i=0;$i<$num_coach_rating_type;$i++){
		
			$total_rating=$total_rating+$rating[$i];
			
			/*if($rating[$i]!=0 && $get_comment[$i]!=''){	*/		
				$CoachRatingSql="INSERT INTO ".TABLEPREFIX."_user_rating
								 (
								 user_review_id,
								 rcat_id,
								 rating
								 )
								 VALUES
								 (
								 '".$user_review_id."',
								 '".$rating_cat_id[$i]."',
								 '".$rating[$i]."'
								 )";
				$CoachRating=$UserManagerObjAjax->Execute($CoachRatingSql);
			/*}
			else if($rating[$i]!=0 && $get_comment[$i]==''){
				$CoachRatingSql="INSERT INTO ".TABLEPREFIX."_user_rating
								 (
								 user_review_id,
								 rcat_id,
								 rating,
								 comment
								 )
								 VALUES
								 (
								 '".$user_review_id."',
								 '".$rating_cat_id[$i]."',
								 '".$rating[$i]."',
								 '".$get_comment[$i]."'
								 )";
				$CoachRating=$UserManagerObjAjax->Execute($CoachRatingSql);
			}
		 	else if($rating[$i]==0 && $get_comment[$i]!=''){
				$CoachRatingSql="INSERT INTO ".TABLEPREFIX."_user_rating
								 (
								 user_review_id,
								 rcat_id,
								 rating,
								 comment
								 )
								 VALUES
								 (
								 '".$user_review_id."',
								 '".$store_id[$i]."',
								 '".$rating[$i]."',
								 '".$get_comment[$i]."'
								 )";
				$CoachRating=$UserManagerObjAjax->Execute($CoachRatingSql);
			}*/
		}
		
		if($CoachRating){
		
			/*$avg_rating=ceil($total_rating/$num_coach_rating_type);
			
			$UpdateCoachReviewSql="UPDATE ".TABLEPREFIX."_user_review SET rating='".$avg_rating."',is_active='Y' WHERE user_review_id=".$user_review_id;
			$UpdateCoachReview=$UserManagerObjAjax->Execute($UpdateCoachReviewSql);*/
			
			$totreview = "SELECT count(*),sum(rating) as totrate FROM ".TABLEPREFIX."_user_review WHERE user_id='".$coach_id."' AND is_active='Y'";
            $totArr = $UserManagerObjAjax->GetRecords("Row",$totreview);
		    $tot_count = $totArr[0];
            $tot_rate = $totArr['totrate'];
		    $avg = round($tot_rate/$tot_count);
			
	        $UpdateSql = "UPDATE ".TABLEPREFIX."_user_type_user_relation SET overall_rating='".$avg."' WHERE user_id='".$coach_id."' and user_type_id='".$user_type_id."'";
	        $UserManagerObjAjax->Execute($UpdateSql);
		}
	}
	
	/*$Sql = "INSERT INTO ".TABLEPREFIX."_user_review set reviewed_by='".$_SESSION['user_id']."',user_id='$coach_id',user_type_id='1',review_comment='$review_comment',
	rating='$rating',is_active='Y',date_added='".date('Y-m-d')."'";
	$UserManagerObjAjax->Execute($Sql);
	if(!mysql_error())
	{
		  $totreview = "SELECT count(*),sum(rating) as totrate FROM ".TABLEPREFIX."_user_review WHERE user_id='".$coach_id."' AND is_active='Y'";
          $totArr = $UserManagerObjAjax->GetRecords("Row",$totreview);
		  $tot_count = $totArr[0];
          $tot_rate = $totArr['totrate'];
		  $avg = round($tot_rate/$tot_count);
	      $UpdateSql = "UPDATE ".TABLEPREFIX."_user_type_user_relation SET overall_rating='$avg' WHERE user_id='$coach_id'";
	      $UserManagerObjAjax->Execute($UpdateSql);*/
		  /* UPDATE over all rating starts */
		  
		   /* send email notification starts  */
/*			if($_SESSION['user_type'] =='1')
			  $type = 'Coach :';
			else
			  $type = 'Student :';  
			
			$usertoSql="SELECT * FROM ".TABLEPREFIX."_user where user_id='".$coach_id."'";
			$recordto = $UserManagerObjAjax->GetRecords("Row",$usertoSql);
			
			$ContactSql = "SELECT admin_email FROM ".TABLEPREFIX."_admin WHERE  admin_id=1";
            $Contact_email = $UserManagerObjAjax->GetRecords("Row",$ContactSql);
			$admin_email=$Contact_email['admin_email'];
			
			$tpl_file="email_templates/review_notification.html";
			$tpl_handler=fopen($tpl_file,"r");
			$tpl_message=fread($tpl_handler,filesize($tpl_file));
			fclose($tpl_handler);
		
				$fromEmail	= "GEC Admin";
				$to			= $admin_email;
		       
				 
				$url=$site_url;
				$tpl_message=str_replace("[SITEURL]",$url,$tpl_message);
				$tpl_message=str_replace("[HEADING]",'Administrator',$tpl_message);
				$tpl_message=str_replace("[BY]",$type.$_SESSION['name'],$tpl_message);
				$tpl_message=str_replace("[FOR]",'Coach : '.$recordto['name'],$tpl_message);
				$tpl_message=str_replace("[REVIEW]",$review_comment,$tpl_message);
				$tpl_message=str_replace("[RATING]",$rating,$tpl_message);
			
				$mail = new PHPMailer;
				$mail->FromName = "GEC Admin";
				$mail->From    =  $fromEmail;
				$mail->Subject =  "Review post by ".$_SESSION['name'];
				$mail->Body    = stripslashes($tpl_message);
				$mail->IsHTML(true);
				$mail->AddAddress($to,$to);
				
			   if($_SERVER['HTTP_HOST'] !='10.10.0.100')
				$mail->Send();
*/		
			  /* send email notification ends */
	   
	   echo json_encode(

				array(

					'flag'	=>	1,
					'user_id' => $reviewed_by,
					'coach_id' =>  $coach_id,
					'sql' => $avg
				)
			);
	//}		

}

?>